// 2025/5/28
// 拜访

#include <climits>
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param CityMap int整型vector<vector<>> 
     * @param n int整型 
     * @param m int整型 
     * @return int整型
     */
    int ans = 0, minLen = INT_MAX;
    void dfs(vector<vector<int>>& CityMap, int x, int y, int cur)
    {
        if(CityMap[x][y] == 2 || cur > minLen)
        {
            if(cur < minLen)
            {
                ans = 1;
                minLen = cur;
            }
            else if(cur == minLen)
                ans++;
            return;
        }
        CityMap[x][y] = -1;
        if(x > 0 && CityMap[x - 1][y] != -1)
            dfs(CityMap, x - 1, y, cur + 1);
        if(x < CityMap.size() - 1 && CityMap[x + 1][y] != -1)
            dfs(CityMap, x + 1, y, cur + 1);
        if(y > 0 && CityMap[x][y - 1] != -1)
            dfs(CityMap, x, y - 1, cur + 1);
        if(y < CityMap[0].size() - 1 && CityMap[x][y + 1] != -1)
            dfs(CityMap, x, y + 1, cur + 1);
        CityMap[x][y] = 0;
    }
    
    int countPath(vector<vector<int> >& CityMap, int n, int m) {
        // write code here
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                if(CityMap[i][j] == 1)
                {
                    dfs(CityMap, i, j, 0);
                    return ans;
                }
            }
        }
        return 0;
    }
};